var Subscribe = function () {

    this.$grid_selector = $("#jira_subcribe_grid_table");
    this.pager_selector = "#jira_subcribe_grid_pager";

    // 上次搜索内容
    this.lastSearch = "";

    // 请求url
    this.URL = {
        MODIFY: '/jira/subscribe/modify',
        SUBSCRIBE_GET_ALL: '/jira/subscribe/get/all/mine'
    };

    var self = this;
    this.colModel = [
        {name: 'id', hidden: true, width: 15, key: true},
        {
            name: 'subscribe', sortable: false, width: 40, fixed: true, formatter: function (value, row) {
                return self.operatingTemplate({checked: value ? 'checked' : '', id: row.id})
            }, unformat: function (cellvalue, options, cell) {
                return $(cell).find('input[type="checkbox"]').prop('checked');
            }
        },
        {name: 'projectName', hidden: true, width: 20},
        {
            name: 'keyword', width: 20, formatter: function (value) {
                return _.template('<a target="_blank" href="' + $.Const.JIRA_KEYWORD_URL + '">{{keyword}}</a>')({keyword: value});
            }
        },
        {name: 'summary', width: 20},
        {name: 'statusName', width: 10},
        {
            name: 'startDate', width: 10, formatter: "date",
            formatoptions: {srcformat: 'Y-m-d', newformat: 'Y-m-d'}
        }, {
            name: 'endDate', width: 10, formatter: "date",
            formatoptions: {srcformat: 'Y-m-d', newformat: 'Y-m-d'}
        }, {
            name: 'updated', width: 10, formatter: "date",
            formatoptions: {srcformat: 'Y-m-d', newformat: 'Y-m-d'}
        }
    ];

    this.operatingTemplate = _.template(
        '<input data-subscribe-id="{{id}}" type="checkbox" {{checked}}>'
    );

    this.options = {
        url: this.URL.SUBSCRIBE_GET_ALL,
        pager: this.pager_selector,
        sortname: 'id',
        caption: "订阅列表",
        colNames: ['主键', '订阅', '项目', 'KeyWord', '主题', '状态', '计划开始时间', '计划结束时间', '最后更新时间'],
        colModel: this.colModel,
        navbarDefaultIcon: {
            add: false,
            edit: false,
            view: false,
            del: false
        }
    };

};

Subscribe.prototype = {
    // 初始化表格
    initGird: function () {

        var self = this;
        this.$grid_selector
            .xumf_jqGird(this.options)
            .xumf_jqGird('navGrid', this.pager_selector).jqGrid('navButtonAdd', this.pager_selector,
            {
                caption: '',
                title: "订阅",
                buttonicon: "fa fa-pencil blue",
                onClickButton: function () {
                    self.editUI();
                },
                position: "first"
            })
            .xumf_jqGird('navGrid', this.pager_selector).jqGrid('navButtonAdd', this.pager_selector,
            {
                caption: '',
                title: "搜索",
                buttonicon: "fa fa-search orange",
                onClickButton: function () {
                    self.searchUI();
                },
                position: "first"
            });
    },
    searchUI: function () {
        var self = this;
        layer.open({
            maxmin: false,
            area: [300, 200],
            title: '搜索',
            btn: ['搜索', '取消'],
            content: _.template('<form style="padding:12px;" class="form-inline">\
                        <div class="form-group">\
                            <label>Keyword/主题名&nbsp;</label>\
                            <input class="form-control" value="{{value}}" autocomplete="off" type="text">\
                        </div>\
                    </form>')({value: this.lastSearch}),
            yes: function (index, layero) {
                self.lastSearch = $(layero).find('input').val().trim();
                self.$grid_selector.setGridParam({postData: {searchString: self.lastSearch}}).trigger('reloadGrid');
                layer.close(index);
            }
        })
    },
    editUI: function () {
        var self = this;
        // 显示弹框
        $.xumf.confirm("确认更改订阅状态？", function (index, layero) {
            self.editAction(index);
        });
    },
    editAction: function (index) {
        var data = _.chain(this.$grid_selector.xumf_jqGird("getRowData")).map(function (o) {
            return {
                id: o.id,
                subscribe: o.subscribe
            }
        }).value();
        $.xumf.ajaxAndLoading({
            url: this.URL.MODIFY,
            contentType: 'application/json',
            data: JSON.stringify(data)
        }).done(function (res) {
            res.success && layer.close(index);
            $.xumf.gritter(res);
        });
    }
};

